package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MainActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class VerifyPhoneNextActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private EditText et_phone;
    private TextView tv_get_code;
    private EditText ev_code;
    private EditText et_passWord_one;
    private EditText et_passWord_two;


    private int time = 0;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    time--;
                    if (time == 0) {
                        tv_get_code.setText("获取验证码");
                    } else {
                        tv_get_code.setText("获取验证码" + time + "s");
                        mHandler.sendEmptyMessageDelayed(1, 1000);
                    }
                    break;

                default:
                    break;
            }
        }
    };

    private String newPhone;
    private String type;
    private String Oldcode;
    private String phone;
    private String code;

    private LinearLayout passWork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_phone_next);

        initData();
        initView();
    }

    private void initData() {
        type = getIntent().getStringExtra("type");
        System.out.println("type====== " + type);
        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        et_phone = (EditText) findViewById(R.id.et_phone);
        tv_get_code = (TextView) findViewById(R.id.get_verification_code);
        ev_code = (EditText) findViewById(R.id.verification_code);
        et_passWord_one = (EditText) findViewById(R.id.et_passWord_one);
        et_passWord_two = (EditText) findViewById(R.id.et_passWord_two);

        tv_get_code.setOnClickListener(this);
        findViewById(R.id.qd).setOnClickListener(this);
        passWork = (LinearLayout) findViewById(R.id.passWork);

        if ("手机".equals(type)) {
            title.setText("更换手机号");
            findViewById(R.id.rl_phone).setVisibility(View.VISIBLE);
            passWork.setVisibility(View.GONE);
        } else {
            title.setText("修改密码");
            findViewById(R.id.rl_phone).setVisibility(View.GONE);
            passWork.setVisibility(View.VISIBLE);
        }
    }

    private String pass_two;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //获取验证码
            case R.id.get_verification_code:
                newPhone = et_phone.getText().toString();
                if (!"".equals(newPhone)) {
                    if (newPhone.length() != 11) {
                        MyToast("请输入正确手机号");
                    } else {
                        if (time == 0) {
                            time = 60;
                            mHandler.sendEmptyMessage(1);
                            upload();
                        } else {
                            MyToast("别着急！正在申请验证码中");
                        }
                    }
                } else if ("".equals(newPhone)) {
                    MyToast("手机号不能为空");
                }
                break;

            //确定
            case R.id.qd:
                String inputCode = ev_code.getText().toString();
                String pass_one = et_passWord_one.getText().toString();
                pass_two = et_passWord_two.getText().toString();
                String phone = et_phone.getText().toString();

                if ("手机".equals(type)) {
                    if (!"".equals(inputCode) && !"".equals(phone)) {
                        upLoading();

                    } else if ("".equals(phone)) {
                        MyToast("手机号不能为空");
                    } else if (phone.length() != 11) {
                        MyToast("请输入正确手机号");
                    } else if ("".equals(inputCode)) {
                        MyToast("验证码不能为空");
                    }
                } else {
                    if ("".equals(pass_one)) {
                        MyToast("密码不能为空");
                    } else if ("".equals(pass_two)) {
                        MyToast("密码不能为空");
                    } else if (!pass_one.equals(pass_two)) {
                        MyToast("您两次输入的密码不一致");
                    } else {
                        uploadPass();
                    }
                }
                break;

            default:
                break;
        }
    }

    private static final String TAG = "VerifyPhoneNextActivity";

    private void uploadPass() {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("code", code);
        map.put("password", pass_two);
        XUtil.Post(Url.USER_CHANGEPASS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: password= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String message = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(message);
                        SharedPreferences sp = getSharedPreferences("login", 0);
                        sp.edit().clear().commit();
                        MyApplication.getInstance().userBean = null;
                        Intent intent = new Intent(VerifyPhoneNextActivity.this, MainActivity.class);
                        intent.putExtra("Pagination", 0);
                        startActivity(intent);
                        finish();
                    } else {
                        MyToast(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void upLoading() {

    }

    private void upload() {

    }
}
